/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.lom2owl.owl;

import java.io.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;

import org.ieee.ltsc.datatype.Duration.*;
import org.ieee.ltsc.lom.LOM;
import org.ieee.ltsc.lom.LOMUtil;

/**
 * Translates a LOM duration element to its representation in OWL. 
 */
public class D2DTranslator extends ElementTranslatorImpl {

    private String category =
            new String();
    private String attributeName =
            new String();
    private String element =
            new String();

    /** Creates a new instance of D2DTranslator */
    public D2DTranslator(String elementCategory, String elementElement,
            String elementAttribute) {
        this.category =
                elementCategory;
        this.attributeName =
                elementAttribute;
        this.element =
                elementElement;
    }

    /**
     * This method translates a LOM duration element to its representation 
     * in OWL.
     * @param lom The metadata record
     * @param ont The ontology
     * @param inst The instance of learning Object
     * @param nsName The namespace
     * @param errorWriter The error file
     */
    public void apply(LOM lom, OntModel ont, Individual inst, String nsName,
            PrintWriter errorWriter) {
        org.ieee.ltsc.lom.LOM.Technical.Duration tDuration =
                null;

        String instance =
                null;

        if (category.equals("Technical")) {
            //4.7 duration
            if (element.equals("Duration")) {

                tDuration =
                        LOMUtil.getTechnicalDuration(lom);

                //create the duration instance
                OntClass durationClass =
                        ont.getOntClass(nsName +
                        "#lomDuration");

                if (tDuration != null) {

                    //duration value
                    String durLOM =
                            String.valueOf(LOMUtil.getDuration(tDuration.value()));

                    instance =
                            "TechD" +
                            inst.getLocalName();
                    Individual durationInstance =
                            ont.createIndividual(nsName +
                            instance, durationClass);
                    DatatypeProperty durationProperty =
                            ont.getDatatypeProperty(nsName + "#hasDurationValue");

                    if (durLOM.compareTo("null") != 0) {
                        RDFNode durationValue =
                                ont.createTypedLiteral(durLOM);
                        durationInstance.setPropertyValue(durationProperty,
                                durationValue);
                    }
                    //duration description (langstring)
                    OntClass langStringClass =
                            ont.getOntClass(nsName +
                            "#langString");
                    OntClass singleLangStringClass =
                            ont.getOntClass(nsName +
                            "#singleLangString");

                    instance =
                            "lsTechD" + inst.getLocalName();

                    Individual langInstance =
                            ont.createIndividual(nsName +
                            instance, langStringClass);

                    ObjectProperty includeLangStringProperty =
                            ont.getObjectProperty(nsName +
                            "#includesSingleLangString");
                    DatatypeProperty hasValueProperty =
                            ont.getDatatypeProperty(nsName + "#hasValue");
                    DatatypeProperty inLanguageProperty =
                            ont.getDatatypeProperty(nsName + "#inLanguage");


                    int i =
                            0;
                    while ((LOMUtil.getString(LOMUtil.getDescription(tDuration),
                            i)) != null) {
                        instance =
                                "slsTechD" + inst.getLocalName() + "-" +
                                String.valueOf(i);

                        Individual singleInstance =
                                ont.createIndividual(nsName + instance,
                                singleLangStringClass);
                        String elementLOM =
                                LOMUtil.getString(LOMUtil.getTechnicalDuration(lom).
                                description(), i);
                        String languageLOM =
                                LOMUtil.getLanguage(LOMUtil.getTechnicalDuration(lom).
                                description(), i);

                        if (elementLOM != null) {
                            RDFNode elementValue =
                                    ont.createTypedLiteral(elementLOM);
                            singleInstance.setPropertyValue(hasValueProperty,
                                    elementValue);
                        } else {
                            String log =
                                    "The Description field of the Technical category must contain a value";
                            errorWriter.println(log);
                        }
                        if (languageLOM != null) {
                            RDFNode languageValue =
                                    ont.createTypedLiteral(languageLOM);
                            singleInstance.setPropertyValue(inLanguageProperty,
                                    languageValue);
                        } else {
                            String log =
                                    "The Language field of Description in the Technical category must contain a value";
                            errorWriter.println(log);
                        }
                        langInstance.addProperty(includeLangStringProperty,
                                singleInstance);
                        i++;
                    }
                    ObjectProperty descriptionProperty =
                            ont.getObjectProperty(nsName +
                            "#hasDurationDescription");
                    durationInstance.setPropertyValue(descriptionProperty,
                            langInstance);

                    ObjectProperty hasDurationProperty =
                            ont.getObjectProperty(nsName + "#hasDuration");
                    inst.setPropertyValue(hasDurationProperty, durationInstance);

                }
            }

        }


    }
}

